package handler

import (
	"context"
	model "gitee.com/duan_shengjie/category/domain/model"
	"gitee.com/duan_shengjie/category/domain/repository"
	category "gitee.com/duan_shengjie/category/proto/category"
)

type Category struct {
	CategoryRepository repository.ICategoryRepository
}

func (c *Category) CreateCategory(ctx context.Context, req *category.CategoryReq, res *category.CreateCategoryRes) error {
	m := &model.Category{
		CategoryName:        req.CategoryName,
		CategoryLevel:       req.CategoryLevel,
		CategoryParent:      req.CategoryParent,
		CategoryImage:       req.CategoryImage,
		CategoryDescription: req.CategoryDescription,
	}
	_, err := c.CategoryRepository.Add(m)
	if err != nil {
		res.Message = "创建失败"
		return err
	}
	res.Message = "创建成功"
	return err
}

func (c *Category) UpdateCategory(ctx context.Context, req *category.CategoryReq, res *category.UpdateCategoryRes) error {
	m := &model.Category{
		CategoryName:        req.CategoryName,
		CategoryLevel:       req.CategoryLevel,
		CategoryParent:      req.CategoryParent,
		CategoryImage:       req.CategoryImage,
		CategoryDescription: req.CategoryDescription,
	}
	err := c.CategoryRepository.Update(m)
	if err != nil {
		res.Message = "更新失败"
		return err
	}
	res.Message = "更新成功"
	return err
}

func (c *Category) DeleteCategory(ctx context.Context, req *category.DeleteCategoryReq, res *category.DeleteCategoryRes) error {
	err := c.CategoryRepository.Delete(req.CategoryId)
	if err != nil {
		res.Message = "删除失败"
		return err
	}
	res.Message = "删除成功"
	return err
}

func (c *Category) FindCategoryByName(ctx context.Context, req *category.FindByNameReq, res *category.CategoryRes) error {
	m, err := c.CategoryRepository.FindByName(req.CategoryName)
	if err != nil {
		return err
	}
	res = FormatCategoryRes(m)
	return err
}

func (c *Category) FindCategoryByID(ctx context.Context, req *category.FindByIDReq, res *category.CategoryRes) error {
	m, err := c.CategoryRepository.FindById(req.CategoryId)
	if err != nil {
		return err
	}
	res = FormatCategoryRes(m)
	return err
}

func (c *Category) FindCategoryByLevel(ctx context.Context, req *category.FindByLevelReq, res *category.FindAllRes) error {
	list, err := c.CategoryRepository.FindByLevel(req.Level)
	if err != nil {
		return err
	}
	res = FormatCategoryResList(list)
	return err
}

func (c *Category) FindCategoryByParent(ctx context.Context, req *category.FindByParentReq, res *category.FindAllRes) error {
	list, err := c.CategoryRepository.FindByParent(req.ParentId)
	if err != nil {
		return err
	}
	res = FormatCategoryResList(list)
	return err
}

func (c *Category) FindAllCategory(ctx context.Context, req *category.FindAllReq, res *category.FindAllRes) error {
	list, err := c.CategoryRepository.FindAll()
	if err != nil {
		return err
	}
	res = FormatCategoryResList(list)
	return err
}

func FormatCategoryRes(m *model.Category) (res *category.CategoryRes) {
	res = new(category.CategoryRes)
	res.CategoryDetail.CategoryName = m.CategoryName
	res.CategoryDetail.CategoryLevel = m.CategoryLevel
	res.CategoryDetail.CategoryImage = m.CategoryImage
	res.CategoryDetail.CategoryParent = m.CategoryParent
	res.CategoryDetail.CategoryDescription = m.CategoryDescription
	return
}
func FormatCategoryResList(list []*model.Category) (resList *category.FindAllRes) {
	for _, item := range list {
		res := FormatCategoryRes(item)
		resList.Category = append(resList.Category, res)
	}
	return
}
